// proxy代理对象
const hd =  {
    name: 'zhangsan',
    age: 39
};

const proxy = new Proxy(hd, {
    get(obj, property) {
        console.log('proxy------get:', property);
        return obj[property];
    },
    set(obj, property, value) {
        console.log('proxy------set:', property, value);
        obj[property] = value;
    }
});
proxy.age = 55;
console.log('proxy代理对象:', hd, proxy, proxy.age);


// proxy 代理函数
function dofn(num) {
    return num === 1 ? 1 : num * dofn(num - 1);
};

let proxyFn = new Proxy(dofn, {
    apply(func, obj, ags) {
        console.time('run');
        let res = func.apply(obj, ags);
        console.log('proxy----apply:', func);
        console.timeEnd('run');
        return res;
    }
});

let res = proxyFn.apply({}, [100]);
console.log('proxy 代理函数=>apply=>', res);


// proxy代理数组
let lesson = [
    {
        name: 'js',
        price: 100,
        color: 'red'
    },
    {
        name: 'html',
        price: 50,
        color: 'green'
    },
    {
        name: 'css',
        price: 49,
        color: 'blue'
    }
];
let proxyArr = new Proxy(lesson, {
    get(Array, key) {
        console.log('--------paroxyArr----get---');
        return Array[key];
    },
    // set(Array, key, value) {
    //     console.log('--------paroxyArr----set');
    //     return Array[key] = value;
    // }
});
proxyArr[2].price = 55;
console.log(' proxy代理数组:', proxyArr[1], proxyArr)